﻿$(document).ready(function () {
    $(document).ajaxStart(showAjaxLoading).ajaxStop(hideAjaxLoading);
});


function checkSubmit() {
    var errFlag = 0;
    errFlag = checkUserNameValid() ? errFlag : errFlag + 1;
    errFlag = checkVerify() ? errFlag : errFlag + 1;
  
    errFlag = checkUserName() ? errFlag : errFlag + 1;
    errFlag = checkEmail() ? errFlag : errFlag + 1;
    errFlag = checkPassword() ? errFlag : errFlag + 1;
    errFlag = checkComfirmPassword() ? errFlag : errFlag + 1;
    
    return errFlag == 0;
}

function checkUserNameValid() {
    var username = $.trim($('#userName').val());
    if (checkUserName()) {
        $.post('/UCenter/Ajax.ashx?a=che_u', { userName: username, e: Math.random().toString() }, function (data) {
            if (data.success) {
                $('#usernamevalid_msg_succ').show();
                $('#usernamevalid_msg_error').hide();
                $('#username_msg').hide();
                return true;
            } else {
                $('#usernamevalid_msg_succ').hide();
                if (data.error != 'null') {
                    $('#usernamevalid_msg_error').hide();
                    $('#username_msg').show();
                    $('#username_msg').html(data.error);
                }
                else {
                    $('#usernamevalid_msg_error').show();
                    $('#username_msg').hide();
                }
                return false;
            }
        }, 'json');
    }
    return true;
}
//验证用户名
function checkUserName() {
    var username = $.trim($('#userName').val());
    if (username == "" || username.length < 6) {
        $('#usernamevalid_msg_succ').hide();
        $('#usernamevalid_msg_error').hide();
        $('#username_msg').show();
        $('#username_msg').html('请输入至少6位的用户名!');
        return false;
    } else {
        $('#usernamevalid_msg_error').hide();
        $('#username_msg').hide();
        return true;
    }
}
//验证邮件
function checkEmail(){
    var email=$.trim($('#email').val());
    if(email=="" || ( email!="" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(email) ) ){
        $('#email_msg').show();
        $('#email_msg').html('请输入正确的E-Mail地址!');
        return false;
    }else{
        $('#email_msg').hide();
        return true;
    }
}
function checkPassword() {
    var password = $.trim($('#password').val());
    if (password == '') {
        $('#password_msg').show();
        $('#password_msg').html('密码不能为空!');
        return false;
    } else {
        $('#password_msg').hide();
        return true;
    }
}
function checkComfirmPassword() {
    if ($.trim($('#password').val()) == '' || ($.trim($('#password').val()) != $.trim($('#comfirmPassword').val()))) {
        $('#comfirmPassword_msg').show();
        $('#comfirmPassword_msg').html('密码不能为空,或两次密码不正确!');
        return false;
    } else {
        $('#comfirmPassword_msg').hide();
        return true;
    }
}

function checkVerify(){
    var verifycode = $.trim($('#verifyCode').val());
    if (verifycode != '') {
        $.post('/UCenter/Ajax.ashx?a=che_c', { verifyCode: verifycode, e: Math.random().toString() }, function (data) {
            if (data.success) {
                $('#verifyCode_msg').hide();
                return true;
            } else {
                $('#verifyCode_msg').show();
                if (data.error != 'null') {
                    $('#verifyCode_msg').html(data.error);
                }
                else {
                    $('#verifyCode_msg').html('验证码不正确！');
                }
                return false;
            }
        }, 'json');
    } else {
        $('#verifyCode_msg').show();
        $('#verifyCode_msg').html('验证码为空！');
    }
    return true;
}



function showAjaxLoading(){	
	$('#ajaxLoading').css({ 
		top: $(window).scrollTop() + 5 + 'px'
	}).show();
}

function hideAjaxLoading(){
	$('#ajaxLoading').hide();
}
